package com.youfan.control;

import com.alibaba.fastjson.JSONObject;
import com.youfan.entity.YoufanProduct;
import com.youfan.page.CommonPage;
import com.youfan.re.Result;
import com.youfan.service.ProductService;
import com.youfan.service.ProductServiceRedis;
import com.youfan.utils.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@RestController
@RequestMapping(value = "product")
public class ProductControl {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductServiceRedis productServiceRedis;


    @Value("${picturepath}")
    private String picturepath;



    @RequestMapping(method = RequestMethod.POST,value = "add")
    @ResponseBody
    public Result add(@RequestParam("picfile") MultipartFile picfile,
                      @RequestParam(value = "formdata" ) String formdata)
    {
        try {
            if (picfile.isEmpty()||picfile.isEmpty()) {
                throw new Exception("上传文件不能为空");
            }
            YoufanProduct youfanProduct = JSONObject.parseObject(formdata, YoufanProduct.class);
            /**
             * 图片处理
             */
            String oldPicFileName = picfile.getOriginalFilename();
            String path = picturepath+"/product/pic";

            File file = new File(path);
            if (!file.isDirectory()){
                file.mkdirs();
            }
            String newName = UUID.randomUUID().toString() +oldPicFileName.substring(oldPicFileName.indexOf("."), oldPicFileName.length());
            File newFile = new File(file, newName);
            picfile.transferTo(newFile);
            youfanProduct.setPic("/product/pic/"+newName);
            productService.insertOne(youfanProduct);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }


    @RequestMapping(method = RequestMethod.POST,value = "list")
    @ResponseBody
    public Result list(@RequestBody YoufanProduct youfanProduct){
        try {
            if (youfanProduct.getPageNum() == null) {
                youfanProduct.setPageNum(1);
            }
            if (youfanProduct.getPageSize() == null) {
                youfanProduct.setPageSize(5);
            }
            CommonPage<YoufanProduct> resultdata = productService.queryByCondtion(youfanProduct);

            return Result.success(resultdata, "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败"+e.getMessage());
        }
    }


    @RequestMapping(method = RequestMethod.POST,value = "view")
    @ResponseBody
    public Result<YoufanProduct> view(@RequestBody YoufanProduct youfanProduct)
    {
        try {
            YoufanProduct youfanProduct1 = productService.findByid(youfanProduct);
            return Result.success(youfanProduct1,"查询单个对象成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询单个对象失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "update")
    @ResponseBody
    public Result<YoufanProduct> update(@RequestParam("picfile") MultipartFile picfile,
                         @RequestParam(value = "formdata" ) String formdata)
    {
        try {

            YoufanProduct youfanProduct = JSONObject.parseObject(formdata, YoufanProduct.class);
            long id = youfanProduct.getId();
            YoufanProduct youfanProduct1 = productService.findByid(youfanProduct);
            CopyUtils.copyProperties(youfanProduct,youfanProduct1);
            if ( null != picfile && !picfile.isEmpty()) {
                /**
                 * 图片处理
                 */
                String picFileName = picfile.getOriginalFilename();
                String path = picturepath+"/product/pic";

                File file = new File(path);
                if (!file.isDirectory()){
                    file.mkdirs();
                }
                String newName = UUID.randomUUID().toString() +picFileName.substring(picFileName.indexOf("."), picFileName.length());
                File newFile = new File(file, newName);
                picfile.transferTo(newFile);
                youfanProduct1.setPic("/product/pic/"+newName);
            }

            YoufanProduct youfanProduct2=productService.updateOne(youfanProduct1);
            productServiceRedis.updateProduct(youfanProduct2);
            return Result.success(youfanProduct2,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "updateWithoutFile")
    @ResponseBody
    public Result<YoufanProduct> updateWithoutFile(@RequestParam(value = "formdata" ) String formdata)
    {
        try {

            YoufanProduct youfanProduct = JSONObject.parseObject(formdata, YoufanProduct.class);
            long id = youfanProduct.getId();
            YoufanProduct youfanProduct1 = productService.findByid(youfanProduct);
            CopyUtils.copyProperties(youfanProduct,youfanProduct1);
            YoufanProduct youfanProduct2=productService.updateOne(youfanProduct1);
            return Result.success(youfanProduct2,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }


}
